<?php
/**
 * Created by PhpStorm.
 * User: seven
 * Date: 14-12-18
 * Time: 下午2:44
 */
include "HttpClient.class.php";
class FeiyinAPI{
    var $member_code;
    var $feyin_key;
    var $device_no;
    var $feyin_host;
    var $feyin_port;
    var $req_time;
    var $mode;
    var $msgNo;
    var $charge;
    var $parameters;//请求参数，类型为关联数组


    function __construct(){
        $this->member_code = 'a972a740867111e4959400163e02163b';
        $this->feyin_key = '106835f9';
        $this->device_no = '4600245120073687';
//        $this->device_no = '9477588613029922';
        $this->feyin_host = 'my.feyin.net';
        $this->feyin_port = 80;
        $this->mode = 2;
        $this->req_time = number_format(1000*time(), 0, '', '');
    }

    function trimString($value)
    {
        $ret = null;
        if (null != $value)
        {
            $ret = $value;
            if (strlen($ret) == 0)
            {
                $ret = null;
            }
        }
        return $ret;
    }

    function setParameter($parameter, $parameterValue) {
        $this->parameters[$this->trimString($parameter)] = $this->trimString($parameterValue);
    }

    function formatParatostring(){
        $msg['memberCode'] = $this->member_code;
        foreach($this->parameters as $key => $val){
            $msg[$key] = $val;
        }
        $msg['deviceNo'] = $this->device_no;
        $msg['msgNo'] = $this->msgNo;
        $msg['reqTime'] = $this->req_time;
        $content = implode('', $msg);
        $content .= $this->feyin_key;
        $msg['securityCode'] = md5($content);
        $msg['mode'] = $this->mode;
        if($this->mode == 1) $msg['charge'] = $this->charge;

        //print_r($msg);

        //print_r($content);
        return $msg;
    }

    function setMsgNo($msgNo){
        $this->msgNo = $msgNo;
    }

    function setCharge($charge){
        $this->charge = $charge;
    }



    //----------------------以下是接口定义实现，第三方应用可根据具体情况直接修改----------------------------

    /*function sendFreeMessage($msg) {
        $msg['reqTime'] = number_format(1000*time(), 0, '', '');
        $content = $msg['memberCode'].$msg['msgDetail'].$msg['deviceNo'].$msg['msgNo'].$msg['reqTime'].$this->feyin_key;
        $msg['securityCode'] = md5($content);
        $msg['mode']=2;

        return sendMessage($msg);
    }

    function sendFormatedMessage($msgInfo) {
        $msgInfo['reqTime'] = number_format(1000*time(), 0, '', '');
        $content = $msgInfo['memberCode'].$msgInfo['customerName'].$msgInfo['customerPhone'].$msgInfo['customerAddress'].$msgInfo['customerMemo'].$msgInfo['msgDetail'].$msgInfo['deviceNo'].$msgInfo['msgNo'].$msgInfo['reqTime'].FEYIN_KEY;

        $msgInfo['securityCode'] = md5($content);
        $msgInfo['mode']=1;

        return sendMessage($msgInfo);
    }*/


    function sendMessage() {
        $client = new HttpClient($this->feyin_host,$this->feyin_port);
//        print_r($client);
        $msgInfo = $this->formatParatostring();
        if(!$client->post('/api/sendMsg',$msgInfo)){ //提交失败
            return 'faild';
        }
        else{
            return $client->getContent();
        }
    }

    function queryState($msgNo){
        $now = number_format(1000*time(), 0, '', '');
        $client = new HttpClient($this->feyin_host,$this->feyin_port);
        if(!$client->get('/api/queryState?memberCode='.$this->member_code.'&reqTime='.$now.'&securityCode='.md5($this->member_code.$now.$this->feyin_key.$msgNo).'&msgNo='.$msgNo)){ //请求失败
            return 'faild';
        }
        else{
            return $client->getContent();
        }
    }

    function listDevice(){
        $now = number_format(1000*time(), 0, '', '');
        $client = new HttpClient($this->feyin_host,$this->feyin_port);
        if(!$client->get('/api/listDevice?memberCode='.$this->member_code.'&reqTime='.$now.'&securityCode='.md5($this->member_code.$now.$this->feyin_key))){ //请求失败
            return 'faild';
        }
        else{
            /***************************************************
            解释返回的设备状态
            格式：
            <device id="4600006007272080">
            <address>广东**</address>
            <since>2010-09-29</since>
            <simCode>135600*****</simCode>
            <lastConnected>2011-03-09  19:39:03</lastConnected>
            <deviceStatus>离线 </deviceStatus>
            <paperStatus></paperStatus>
            </device>
             **************************************************/

            $xml = $client->getContent();
            $sxe = new SimpleXMLElement($xml);
            foreach($sxe->device as $device) {
                $id = $device['id'];
                echo "设备编码：$id    ";

                $deviceStatus = $device->deviceStatus;
                echo "状态：$deviceStatus";
                echo '<br>';
            }
        }
    }


    function listException(){
        $now = number_format(1000*time(), 0, '', '');
        $client = new HttpClient($this->feyin_host,$this->feyin_port);
        if(!$client->get('/api/listException?memberCode='.$this->member_code.'&reqTime='.$now.'&securityCode='.md5($this->member_code.$now.$this->feyin_key))){ //请求失败
            return 'faild';
        }
        else{
            return $client->getContent();
        }
    }

}